/**
 * TopBar.java
 * com.jamin.topbar
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-4 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.custom_view_topbar_path;
/**
 * ClassName:TopBar
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-4 上午8:14:21 
 */

import com.jamin.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class TopBar extends RelativeLayout{
	private Button leftBtn,rightBtn;
	private TextView title;
	private TopBarClickListener topBarClickListener;
	private String titleStr;

	private RelativeLayout.LayoutParams leftBtnLayoutParams,rightBtnLayoutParams,titleLayoutParams;
	private static int LEFT_BTN_ID = 1;
	private static int TITLE_ID = 2;
	private static int RIGHT_BTN_ID = 3;

	private Drawable leftBackground,rightBackground;
	private String leftText,rightText;
	private int leftTextColor,rightTextColor,titleTextColor;
	private int leftVisibility,rightVisibility;
	private float titleTextSize;

	public TopBar(Context context,AttributeSet attrs){
		super(context,attrs);
		//set TopBar layout
		LayoutParams topBarLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		setLayoutParams(topBarLayoutParams);
		//get attr
		TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
		this.titleStr = ta.getString(R.styleable.TopBar_title);
		this.leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
		this.rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
		this.leftText = ta.getString(R.styleable.TopBar_leftText);
		this.rightText = ta.getString(R.styleable.TopBar_rightText);
		this.leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
		this.rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);
		this.leftVisibility = ta.getInteger(R.styleable.TopBar_leftVisibility, 0);
		this.rightVisibility = ta.getInteger(R.styleable.TopBar_rightVisibility, 0);
		this.titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 14);
		this.titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);
		ta.recycle();
		//get widget instance
		leftBtn = new Button(context);
		rightBtn = new Button(context);
		title = new TextView(context);

		leftBtn.setId(LEFT_BTN_ID);
		rightBtn.setId(RIGHT_BTN_ID);
		title.setId(TITLE_ID);

		leftBtn.setVisibility(this.leftVisibility);
		rightBtn.setVisibility(this.rightVisibility);

		leftBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		rightBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		titleLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

		leftBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
		leftBtnLayoutParams.setMargins(12, 0, 0, 0);//左上右下
		leftBtnLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		rightBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
		rightBtnLayoutParams.setMargins(0, 0, 12, 0);//左上右下
		rightBtnLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);


		//		titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
		//		titleLayoutParams.setMargins(0, 0, 12, 0);//左上右下
		//		titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		//		titleLayoutParams.addRule(RelativeLayout.LEFT_OF, RIGHT_BTN_ID);
		//		titleLayoutParams.addRule(RelativeLayout.RIGHT_OF, LEFT_BTN_ID);
		titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

		//add view
		addView(leftBtn, leftBtnLayoutParams);
		addView(rightBtn,rightBtnLayoutParams);
		addView(title,titleLayoutParams);

		leftBtn.setBackgroundDrawable(leftBackground);
		leftBtn.setText(leftText);
		leftBtn.setTextColor(leftTextColor);
		rightBtn.setBackgroundDrawable(rightBackground);
		rightBtn.setText(rightText);
		rightBtn.setTextColor(rightTextColor);

		title.setTextSize(22.0f);
		title.setTextColor(Color.BLUE);
		title.setEllipsize(TruncateAt.MIDDLE);
		title.setGravity(Gravity.CENTER_HORIZONTAL);
		title.setSingleLine(true);
		title.setText(titleStr);
		title.setTextSize(titleTextSize);
		title.setTextColor(titleTextColor);

		leftBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(topBarClickListener!=null){
					topBarClickListener.leftBtnClick();
				}
			}
		});

		rightBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(topBarClickListener!=null){
					topBarClickListener.rightBtnClick();
				}
			}
		});
	}

	/**
	 * Set TopBar title;
	 * @method setTitle
	 * @throws 
	 * @since v1.0
	 */
	public void setTitle(String text){
		title.setText(text);
	}



	public void setTopBarClickListener(TopBarClickListener topBarClickListener) {
		this.topBarClickListener = topBarClickListener;
	}




}


